﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace JiaBoPrint
{
    public partial class 订货会直营订货数据 : Form
    {
        private IntPtr Gp_IntPtr;                   //驱动打印句柄
        private libUsbContorl.UsbOperation NewUsb = new libUsbContorl.UsbOperation();
        public 订货会直营订货数据()
        {
            InitializeComponent();
        }
        private void SendData2USB(byte[] str)
        {
            NewUsb.SendData2USB(str, str.Length);
        }
        private void SendData2USB(string str)
        {
            byte[] by_SendData = System.Text.Encoding.GetEncoding(54936).GetBytes(str);
            SendData2USB(by_SendData);
        }
        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)//如果输入的是回车键  
            {
                string dm = textBox1.Text.ToString();
                string sqldm = @"exec [zdy_proc_GetDelOrderByProduct_ZY] '{0}'";
                sqldm = string.Format(sqldm, dm);
                DataTable dtdm = DBHelper_DHH.ExecuteDataTable(sqldm);
                if (dtdm.Rows.Count > 0)
                {
                    NewUsb.FindUSBPrinter();
                    for (int k = 0; k < NewUsb.USBPortCount; k++)
                    {
                        if (NewUsb.LinkUSB(k))
                        {
                            SendData2USB("SIZE 60 mm,80 mm\r\n");//设置标签大小，单位mm.具体参数请用尺子量一下
                            SendData2USB("GAP 2 mm,0 mm\r\n");//设置两个标签之间的间隙，单位mm.具体参数请用尺子量一下
                            SendData2USB("CLS\r\n");//清空缓冲区
                            SendData2USB("DENSITY 7\r\n");//打印浓度
                            SendData2USB("REFERENCE 0,0\r\n");

                            SendData2USB("TEXT 10,10,\"TSS24.BF2\",0,1,1,\"品名:\"\r\n");
                            SendData2USB("TEXT 80,10,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["品名"].ToString() + "\"\r\n");

                            SendData2USB("TEXT 10,50,\"TSS24.BF2\",0,1,1,\"货号:\"\r\n");
                            SendData2USB("TEXT 80,50,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["款号"].ToString() + "\"\r\n");

                            SendData2USB("TEXT 10,90,\"TSS24.BF2\",0,1,1,\"价格:\"\r\n");
                            SendData2USB("TEXT 80,90,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["价格"].ToString() + "\"\r\n");
                            SendData2USB("TEXT 150,90,\"TSS24.BF2\",0,1,1,\"系列:\"\r\n");
                            SendData2USB("TEXT 210,90,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["系列"].ToString() + "\"\r\n");
                            SendData2USB("TEXT 300,90,\"TSS24.BF2\",0,1,1,\"系列排名:\"\r\n");
                            SendData2USB("TEXT 410,90,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["直联营系列排名"].ToString() + "\"\r\n");

                          
                            SendData2USB("TEXT 10,130,\"TSS24.BF2\",0,1,1,\"自营订量:\"\r\n");
                            SendData2USB("TEXT 150,130,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["直联营订量"].ToString() + "\"\r\n");
                            SendData2USB("TEXT 250,130,\"TSS24.BF2\",0,1,1,\"总订量:\"\r\n");
                            SendData2USB("TEXT 340,130,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["总订量"].ToString() + "\"\r\n");

                            SendData2USB("TEXT 10,170,\"TSS24.BF2\",0,1,1,\"直联营品类排名:\"\r\n");
                            SendData2USB("TEXT 200,170,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["直联营品类排名"].ToString() + "\"\r\n");
                            SendData2USB("TEXT 260,170,\"TSS24.BF2\",0,1,1,\"提前下单:\"\r\n");
                            SendData2USB("TEXT 375,170,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["提前下单"].ToString() + "\"\r\n");

                            SendData2USB("TEXT 10,210,\"TSS24.BF2\",0,1,1,\"北京:\"\r\n");
                            SendData2USB("TEXT 80,210,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["北京"].ToString() + "\"\r\n");
                            SendData2USB("TEXT 150,210,\"TSS24.BF2\",0,1,1,\"长春:\"\r\n");
                            SendData2USB("TEXT 220,210,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["长春"].ToString() + "\"\r\n");
                            SendData2USB("TEXT 290,210,\"TSS24.BF2\",0,1,1,\"成都:\"\r\n");
                            SendData2USB("TEXT 360,210,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["成都"].ToString() + "\"\r\n");

                            SendData2USB("TEXT 10,250,\"TSS24.BF2\",0,1,1,\"福建:\"\r\n");
                            SendData2USB("TEXT 80,250,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["福建"].ToString() + "\"\r\n");
                            SendData2USB("TEXT 150,250,\"TSS24.BF2\",0,1,1,\"广州:\"\r\n");
                            SendData2USB("TEXT 220,250,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["广州"].ToString() + "\"\r\n");
                            SendData2USB("TEXT 290,250,\"TSS24.BF2\",0,1,1,\"贵州:\"\r\n");
                            SendData2USB("TEXT 360,250,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["贵州"].ToString() + "\"\r\n");

                            SendData2USB("TEXT 10,290,\"TSS24.BF2\",0,1,1,\"哈尔滨:\"\r\n");
                            SendData2USB("TEXT 95,290,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["哈尔滨"].ToString() + "\"\r\n");
                            SendData2USB("TEXT 150,290,\"TSS24.BF2\",0,1,1,\"湖南:\"\r\n");
                            SendData2USB("TEXT 220,290,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["湖南"].ToString() + "\"\r\n");
                            SendData2USB("TEXT 290,290,\"TSS24.BF2\",0,1,1,\"江西:\"\r\n");
                            SendData2USB("TEXT 360,290,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["江西"].ToString() + "\"\r\n");


                            SendData2USB("TEXT 10,330,\"TSS24.BF2\",0,1,1,\"昆明:\"\r\n");
                            SendData2USB("TEXT 80,330,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["昆明"].ToString() + "\"\r\n");
                            SendData2USB("TEXT 150,330,\"TSS24.BF2\",0,1,1,\"兰州:\"\r\n");
                            SendData2USB("TEXT 220,330,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["兰州"].ToString() + "\"\r\n");
                            SendData2USB("TEXT 290,330,\"TSS24.BF2\",0,1,1,\"内蒙:\"\r\n");
                            SendData2USB("TEXT 360,330,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["内蒙"].ToString() + "\"\r\n");


                            SendData2USB("TEXT 10,370,\"TSS24.BF2\",0,1,1,\"南京:\"\r\n");
                            SendData2USB("TEXT 80,370,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["南京"].ToString() + "\"\r\n");
                            SendData2USB("TEXT 150,370,\"TSS24.BF2\",0,1,1,\"青岛:\"\r\n");
                            SendData2USB("TEXT 220,370,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["青岛"].ToString() + "\"\r\n");
                            SendData2USB("TEXT 290,370,\"TSS24.BF2\",0,1,1,\"上海:\"\r\n");
                            SendData2USB("TEXT 360,370,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["上海"].ToString() + "\"\r\n");

                            SendData2USB("TEXT 10,410,\"TSS24.BF2\",0,1,1,\"深圳:\"\r\n");
                            SendData2USB("TEXT 80,410,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["深圳"].ToString() + "\"\r\n");
                            SendData2USB("TEXT 150,410,\"TSS24.BF2\",0,1,1,\"沈阳:\"\r\n");
                            SendData2USB("TEXT 220,410,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["沈阳"].ToString() + "\"\r\n");
                            SendData2USB("TEXT 290,410,\"TSS24.BF2\",0,1,1,\"石家庄:\"\r\n");
                            SendData2USB("TEXT 375,410,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["石家庄"].ToString() + "\"\r\n");


                            SendData2USB("TEXT 10,450,\"TSS24.BF2\",0,1,1,\"苏州:\"\r\n");
                            SendData2USB("TEXT 80,450,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["苏州"].ToString() + "\"\r\n");
                            SendData2USB("TEXT 150,450,\"TSS24.BF2\",0,1,1,\"太原:\"\r\n");
                            SendData2USB("TEXT 220,450,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["太原"].ToString() + "\"\r\n");
                            SendData2USB("TEXT 290,450,\"TSS24.BF2\",0,1,1,\"天津:\"\r\n");
                            SendData2USB("TEXT 360,450,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["天津"].ToString() + "\"\r\n");

                            SendData2USB("TEXT 10,490,\"TSS24.BF2\",0,1,1,\"西安:\"\r\n");
                            SendData2USB("TEXT 80,490,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["西安"].ToString() + "\"\r\n");
                            SendData2USB("TEXT 150,490,\"TSS24.BF2\",0,1,1,\"新疆:\"\r\n");
                            SendData2USB("TEXT 220,490,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["新疆"].ToString() + "\"\r\n");
                            SendData2USB("TEXT 290,490,\"TSS24.BF2\",0,1,1,\"新乡:\"\r\n");
                            SendData2USB("TEXT 360,490,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["新乡"].ToString() + "\"\r\n");

                            SendData2USB("TEXT 10,530,\"TSS24.BF2\",0,1,1,\"银川:\"\r\n");
                            SendData2USB("TEXT 80,530,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["银川"].ToString() + "\"\r\n");
                            SendData2USB("TEXT 150,530,\"TSS24.BF2\",0,1,1,\"浙江:\"\r\n");
                            SendData2USB("TEXT 220,530,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["浙江"].ToString() + "\"\r\n");
                            SendData2USB("TEXT 290,530,\"TSS24.BF2\",0,1,1,\"郑州:\"\r\n");
                            SendData2USB("TEXT 360,530,\"TSS24.BF2\",0,1,1,\"" + dtdm.Rows[0]["郑州"].ToString() + "\"\r\n");


                            SendData2USB("PRINT 1\r\n");
                            NewUsb.CloseUSBPort();

                            textBox1.Text = "";
                        }
                        else
                        {
                            MessageBox.Show("请确定是否连接上打印机");
                        }
                    }
                }
                else
                {
                    MessageBox.Show("款号不正确");
                }
            }
        }
    }
}
